<?php
// $Id: admin/config.php,v 2.11 2004/12/02 C. Felix alias the Cat
//  ------------------------------------------------------------------------ //
//             XF Guestbook                                                  //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

include '../../../include/cp_header.php';
include_once '../include/cp_functions.php';
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
include_once XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/class/xfgbformselectcountry.php";

function getOptions4Admin($cat=2) {
global $xoopsDB;
	$sql = "SELECT conf_id, conf_name, conf_value, conf_title, conf_formtype, conf_desc  FROM ".$xoopsDB->prefix("xfguestbook_config")." WHERE conf_cat=".$cat." ORDER BY conf_order ASC";
	$result = $xoopsDB->query($sql);
	$i=0;
	while($myrow = $xoopsDB->fetchArray($result)) {
		$arr_conf[$i]['conf_id'] = $myrow['conf_id'];
		$arr_conf[$i]['conf_name'] = $myrow['conf_name'];
		$arr_conf[$i]['conf_value'] = $myrow['conf_value'];
		$arr_conf[$i]['conf_title'] = $myrow['conf_title'];
		$arr_conf[$i]['conf_desc'] = $myrow['conf_desc'];
		$arr_conf[$i]['conf_formtype'] = $myrow['conf_formtype'];
		$i++;
	}
	return $arr_conf;
}

if (isset( $_POST))
    foreach ( $_POST as $k => $v ) {${$k} = $v;} 
	
if (isset( $_GET))
    foreach ( $_GET as $k => $v ) { ${$k} = $v;} 

$op="";

if (isset($_GET['op'])) $op=$_GET['op'];
if (isset($_POST['op'])) $op=$_POST['op'];

switch($op){

case "save":
	$option=getOptions4Admin();
	$nb_opt = count($option);

	for ( $i = 0; $i < $nb_opt; $i++ ) {
  		$sql = "UPDATE ".$xoopsDB->prefix("xfguestbook_config")." SET conf_value='".${$option[$i]['conf_name']}."' WHERE conf_id=".$option[$i]['conf_id'];
 		$result=$xoopsDB->query($sql);
	}
	redirect_header("index.php",1,_AM_XFGB_DBUPDATED);
   	break;

default:
    xoops_cp_header();
	admin_menu(1);
	include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
	
	$option=getOptions4Admin();
	$nb_opt = count($option);

	$sform = new XoopsThemeForm(_AM_XFGB_FORMOPT, "op", xoops_getenv('PHP_SELF'));

	for ( $i = 0; $i < $nb_opt; $i++ ) {
		$title = (!defined($option[$i]['conf_desc']) || constant($option[$i]['conf_desc']) == '') ? constant($option[$i]['conf_title']) : constant($option[$i]['conf_title']).'<br /><br /><span style="font-weight:normal;">'.constant($option[$i]['conf_desc']).'</span>';
		switch ($option[$i]['conf_formtype']) {
		case 'yesno':
			$ele = new XoopsFormRadioYN($title, $option[$i]['conf_name'], $option[$i]['conf_value'], _YES, _NO);
			break;
		case 'selectcountry':
			$ele = new xfgbFormSelectCountry($title, $option[$i]['conf_name'], $option[$i]['conf_value'],1,true);
			break;
		}
		$old_opt = new XoopsFormHidden('old_'.$option[$i]['conf_name'], $option[$i]['conf_value']);
		$hidden = new XoopsFormHidden('conf_ids[]', $option[$i]['conf_id']);
		$sform->addElement($ele);
		$sform->addElement($hidden);
		unset($ele);
		unset($hidden);
	}

	$button_tray = new XoopsFormElementTray('','');
	$hidden = new XoopsFormHidden('op', 'save');
	$button_tray->addElement($hidden);
	$button_tray->addElement(new XoopsFormButton('', 'post', _GO, 'submit'));
	$sform->addElement($button_tray);
	$sform->display();
	unset($hidden);
    break;

}
xoops_cp_footer();
?>
